package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/businesses")
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    @GetMapping
    public Result list(BusinessDto businessDto) {
        PageResult<Business> list = businessService.selectList(businessDto);
        return Result.success(list);
    }

    @PostMapping
    public Result add(@RequestBody Business business) {
        log.info("添加业务{}", business);
        business.setStatus(1);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        businessService.save(business);
        return Result.success();
    }

    @PutMapping("/assign/{businessId}/{userId}")
    public Result update(@PathVariable Integer businessId, @PathVariable Integer userId){
            log.info("修改业务,id={},userId={}",businessId,userId);
            Business business = new Business();
            business.setId(businessId);
            business.setUserId(userId);
            business.setStatus(2);
            business.setUpdateTime(LocalDateTime.now());
            businessService.updateById(business);
            return Result.success();
    }
    @GetMapping("/{id}")
    public Result updateById(@PathVariable Integer id){
        log.info("查询业务,id={}",id);
        Business business =businessService.selectBusById(id);
        return Result.success(business);
    }

    @PutMapping
    public Result updateList(@RequestBody Business business){
        log.info("修改业务,{}",business);
        businessService.updateList(business);
        return Result.success();

    }
}
